<?php
if (!defined("MY_DATABASE_CLASS_PHS")) {
	define("MY_DATABASE_CLASS_PHS",1);

	require_once("Constant888.class.php");
	require_once("Config888.class.php");
	
	$Constant888 = Constant888::instance();
	class MyDatabase {
		protected $server,$user,$password;
		protected $db_link;

		function MyDatabase($server,$user,$password) {
			$this -> initDatabase($server,$user,$password );
		}

		function initDatabase($server,$user,$password) {

			$this-> server = $server;
			$this-> user = $user;
			$this-> password = $password;

			$this-> db_link = mysql_connect($server,$user,$password);
		}

		/**
		 *
		 * @param $dataFieldName e.g: "server","db_link"....
		 * @return depends string or resource, depends on what is returned
		 */
		public function getInfo($dataFieldName) {
			return $this -> $dataFieldName;
		}

		public function query($db_name,$q) {
			mysql_select_db($db_name,$this -> db_link);
			return mysql_query($q);
		}

		public function allResults($db_name,$tbl_name) {
			mysql_select_db($db_name,$this -> db_link);
			return mysql_query("select * from $tbl_name");
		}
	}


	class MyDatabase888 extends MyDatabase {
		protected $db_name,$tbl_name;
		function MyDatabase888() {
			global $Constant888;

			$dbStr = Config888::getAllDatabaseInfo();
			$str = Config888::getAllDatabaseInfo();
			$this->initDatabase($str['server'],$str['username'],$str['password']);
			$this->db_name = $str['db_name'];
			$this->tbl_name = $Constant888 -> value("LOTTO_RESULT_TABLE");
		}

		function getLastNDates($areaCode,$ndays) {
			global $Constant888;
			$query =
		"select DISTINCT RESULT_DATE".
		" from ".$this->tbl_name.
		" where".
		" AREA_CODE = $areaCode and".
		" (select TO_DAYS(MAX(RESULT_DATE)) from ".$this->tbl_name.") - TO_DAYS(RESULT_DATE) <= ".$ndays.
		" order by RESULT_DATE desc";
			/*
			 select DISTINCT RESULT_DATE
			 from store_result
			 where
			 AREA_CODE = 1 and
			 (select TO_DAYS(MAX(RESULT_DATE)) from store_result) - TO_DAYS(RESULT_DATE) <= 7
			 order by RESULT_DATE desc
			 */
			$result = $this -> query($this->db_name,$query);
			$arr = array();
			while ( $row = mysql_fetch_array($result)) {
				$arr[] = $row[0];
			}
			return $arr;

		}
	}
}
?>